package org.gongliang.jsmile.authc.sys.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.gongliang.jsmile.authc.sys.service.SysUserService;
import org.gongliang.jsmile.authc.sys.vo.SysUserVo;
import org.gongliang.jsmile.common.base.BaseController;
import org.gongliang.jsmile.common.base.JSmileResult;
import org.gongliang.jsmile.common.enums.KException;
import org.gongliang.jsmile.common.kit.web.ShiroKit;
import org.gongliang.jsmile.common.kit.web.WebKit;
import org.gongliang.jsmile.config.log.info.KLog;
import org.gongliang.jsmile.config.log.info.LogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @desc   控制器
* @author 龚亮
* @version 1.0
* @date 2018年08月25日 04:57:24
*/
@Controller
@RequestMapping("/sys_user")
@Api(tags = "用户信息管理")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;
    

	/**
	 * 跳转到用户页
	 */
	@GetMapping("/main")
	public String main() {
		return "sys/User/user";
	}

	@GetMapping("/add")
	public String add() {
		return "sys/User/add";
	}

	@GetMapping("/edit")
	public String edit() {
		return "sys/User/edit";
	}


	/**
	 * 修改密码
	 */
	@KLog(title = "用户信息->用户密码修改", type = LogType.UPDATE)
	@ResponseBody
	@PostMapping("/updatePassword")
	@ApiOperation(value = "用户密码修改", notes = "根据原始密码修改密码!")
	public JSmileResult updatePassword(String password, String newPassword) {
		password = new Md5Hash(password, "", 2).toString();
		newPassword = new Md5Hash(newPassword, "", 2).toString();
		if (sysUserService.updatePassword(ShiroKit.getUserId(), password, newPassword)) {
			return JSmileResult.success("密码修改成功!");
		}
		return JSmileResult.error("原密码不正确!");
	}

	/**
	 * 查询所有分页 返回 Lit<Map>
	 */
	@KLog(title = "查询所有用户信息", type = LogType.QUERY)
	@ResponseBody
	@RequestMapping("/findAllByPage")
	@ApiOperation(value = "分页获取用户列表")
	public JSmileResult findAllMapByPage(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		WebKit.setPage(WebKit.toPageIndex(reqMap), WebKit.toPageSize(reqMap));
		List<SysUserVo> list = sysUserService.findAllByVo(reqMap);
		PageInfo<SysUserVo> pages = new PageInfo<SysUserVo>(list);
		JSmileResult result = JSmileResult.success();
		result.setResData(pages.getList(), pages.getTotal());
		return result;
	}

	@KLog(title = "用户信息->添加用户", type = LogType.INSERT)
	@ResponseBody
	@ApiOperation(value = "添加用户信息", notes = "")
	@PostMapping("/save")
	@RequiresPermissions("sys:user:add")
	public JSmileResult save(SysUserVo user) {
		JSmileResult result=JSmileResult.success("添加用户信息成功!");
		String password = new Md5Hash("123456", "admin", 2).toString();
		user.setPassword(password);
		user.setCreateDate(new Date());
		user.setCreator(ShiroKit.getUserId());
		if(!sysUserService.saveORupdate(user)) {
			result=JSmileResult.error("保存用户信息失败!");
		}
		return result;
	}

	@KLog(title = "用户信息->用户信息修改", type = LogType.UPDATE)
	@ResponseBody
	@PostMapping("/update")
	@RequiresPermissions("sys:user:update")
	public JSmileResult update(SysUserVo user) {
		JSmileResult result=JSmileResult.success("用户信息修改成功!");
		user.setUpdater(ShiroKit.getUserId());
		user.setUpdateDate(new Date());
		if(!sysUserService.saveORupdate(user)) {
			result=JSmileResult.error("用户信息修改失败!");
		}
		return result;
	}


	/**
	 * 根据一组id删除
	 */
	@KLog(title = "用户信息->批量删除用户", type = LogType.DELETE)
	@ResponseBody
	@PostMapping("/deleteByIds")
	@ApiOperation(value = "删批量删除用户", notes = "根据url的ids来指定删除对象")
	public JSmileResult deleteByIds(@RequestParam(value = "ids[]", required = true) String[] ids) {
		JSmileResult result=JSmileResult.success("用户删除成功!");
		if (ids == null || ids.length == 0) {
			return JSmileResult.error(KException.IDS_NOT_NULL);
		}
		if (ArrayUtils.contains(ids, ShiroKit.getUserId().toString())) {
			return JSmileResult.error("当前用户不能删除!");
		}
		if (ArrayUtils.contains(ids, "9999")) {
			return JSmileResult.error("管理员不能删除!");
		}
		if(!sysUserService.deleteByIds(ids)) {
			result=JSmileResult.error("用户删除失败!");
		}
		return result;
	}

}
